package cn.jekun.web.shop.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.web.shop.bean.ShopBusiness;

/**
 * 业务查询Service
 * @author Mr.Dong
 *
 */
@Service(BusinessService.BEAN_NAME)
public class BusinessService extends ShopBaseService{
	public static final String BEAN_NAME="cn.jekun.web.shop.service.BusinessService";
	@Override
	public String getNamespace() {
		// TODO Auto-generated method stub
		return "ACTION_BUSINESS_JOURNAL";
	}

	/**
	 * 获取业务流水明细
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List<ShopBusiness> getBusinessDetails(Map map,Page page){
		String SQL ="";
		if(!"".equals(map.get("beginDate")) && "".equals(map.get("endDate"))){
			SQL +=" and a.complete_time"
					+ ">= to_date('"+map.get("beginDate").toString()+"','yyyy/MM/dd hh24:mi:ss')";
		}
		if("".equals(map.get("beginDate")) && !"".equals(map.get("endDate"))){
			SQL +=" and a.complete_time"
					+ "<= to_date('"+map.get("endDate").toString()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}
		if(!"".equals(map.get("beginDate")) && map.get("beginDate")!=null 
				&& !"".equals(map.get("endDate")) && map.get("endDate")!=null){
			SQL +=" and a.complete_time"
					+ ">= to_date('"+map.get("beginDate").toString()+"','yyyy/MM/dd hh24:mi:ss')"
					+ " and a.complete_time "
					+ "<= to_date('"+map.get("endDate").toString()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(map.get("beginMoney")) && "".equals(map.get("endMoney"))){
			SQL +=" and a.amount>="+map.get("beginMoney"); 
		}if("".equals(map.get("beginMoney")) && !"".equals(map.get("endMoney"))){
			SQL +=" and a.amount<="+map.get("endMoney"); 
		}if(map.get("beginMoney")!=null && map.get("endMoney")!=null && !"".equals(map.get("beginMoney")) 
				&& !"".equals(map.get("endMoney"))){
			SQL +=" and a.amount>="+map.get("beginMoney")+" and a.amount<="+map.get("endMoney")+""; 
		}
		if(map.get("busiType")!=null && map.get("busiType").toString().length()>0){
			SQL +=" and c.business_type_id ="+map.get("busiType").toString();
		}
		if(map.get("orderResult")!=null && map.get("orderResult").toString().length()>0){
			SQL +=" and a.result_id ="+map.get("orderResult").toString();
		}
		if(map.get("paymenttype")!=null && map.get("paymenttype").toString().length()>0){
			SQL +=" and a.payment_type_id ="+map.get("paymenttype").toString();
		}
		if(map.get("keyRange")!=null && map.get("keyword_s").toString().length()>0){
			if("bill".equals(map.get("keyRange"))){
				SQL +=" and a.journal_id like '%"+map.get("keyword_s").toString()+"%'";
			}else if("name".equals(map.get("keyRange"))){
				SQL +=" and c.business_name like'%"+map.get("keyword_s").toString()+"%'";
			}
		}
		SqlUtis.prepareSql(map,SQL);

		return this.list(this.getStatementName("list"),map,page);

	}

	/**
	 * 根据业务流水号获取业务详情
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public ShopBusiness getBusinessDetailsById(Map map){
		return this.one(this.getStatementName("querybusinessbyid"), map);
	}

	/**
	 * 根据业务流水号获取业务参数列表
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List<ShopBusiness> getBusinessParamsById(Map map){
		return this.list(this.getStatementName("listbusinessparambyid"), map);
	}
}
